package models

import (
	"errors"
	"github.com/liangchao/blogo/src/common/global"
	"time"
)

type Category struct {
	CategoryId int `json:"category_id" gorm:"primaryKey"`
	CategoryName string `json:"category_name"`
	CategoryIcon string `json:"category_icon"`
	CategoryRank int `json:"category_rank"`
	IsDeleted int `json:"is_deleted"`
	CreateTime *time.Time `json:"create_time"`
	CreateTimeStr string `json:"create_time_str" gorm:"-""`
	UpdateTime *time.Time `json:"update_time"`
	UpdateTimeStr string `json:"update_time_str" gorm:"-"`
}

func (*Category) TableName() string{
	return "tb_blog_category"
}

// GetcategoryById 获取分类通过id
func (c *Category) GetcategoryById() (err error){
	if c.CategoryId < 1 {
		err = errors.New("category id is illegal")
		return
	}

	err = global.DB.Table(c.TableName()).Where("is_deleted = ? and category_id = ?", 0, c.CategoryId).Find(c).Error
	return
}

// GetCategoryAllList 查询全部分类
func (c *Category) GetCategoryAllList() (categories []Category, err error) {
	err = global.DB.Table(c.TableName()).Where("is_deleted = ?", 0).Find(&categories).Error
	return
}